package com.example.administrator.myjdlc.adapter;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.myjdlc.R;
import com.example.administrator.myjdlc.bean.ProductItem;
import com.example.administrator.myjdlc.common.Code;

import java.util.List;

import jp.wasabeef.recyclerview.internal.ViewHelper;

/**
 * Created by Administrator on 2017/4/27.
 */

public class ProductMainHotAdapter extends RecyclerView.Adapter<ProductMainHotAdapter.ViewHolder> {
       private Context ctx;
    private OnItemClickListener mOnItemClickListener = null;
    private float mFrom;
    private static final int[] colors = { R.color.color_21cdb9, R.color.color_fec040,R.color.color_ff7e5a };
    private List<ProductItem> ls_product;
    private RecyclerView mRecyclerView;
    private boolean isFirstOnly =true;
    private int mLastPosition =-1;
    private int mDuration =300;
    private Interpolator mInterpolator = new LinearInterpolator();
    private static final float DEFAULT_SCALE_FROM = .5f;
    private View VIEW_FOOTER;
    private View VIEW_HEADER;

    //Type
    private int TYPE_NORMAL = 1000;
    private int TYPE_HEADER = 1001;
    private int TYPE_FOOTER = 1002;
    public ProductMainHotAdapter(Context ctx, List<ProductItem> products) {
        this(ctx,products,DEFAULT_SCALE_FROM);
    }
    public ProductMainHotAdapter(Context ctx, List<ProductItem> products, float from) {
        this.ctx = ctx;
        this.ls_product = products;
        mFrom = from;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            return new ViewHolder(VIEW_FOOTER);
        } else if (viewType == TYPE_HEADER) {
            return new ViewHolder(VIEW_HEADER);
        } else {
            //        View v =View.inflate(ctx, R.layout.listview_product_item,null);
            final View v = LayoutInflater.from(ctx).inflate(R.layout.listview_product_item, parent, false);
           if (mOnItemClickListener!=null){
               v.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View view) {
                       mOnItemClickListener.onItemClick(view, (Integer) view.getTag());
                   }
               });
           }
            return new ViewHolder(v);
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (!isHeaderView(position) && !isFooterView(position)) {
        if (haveHeaderView()) position--;
        holder.llProductItem.setBackgroundColor(ctx.getResources().getColor(colors[position% colors.length]));
        holder.tvHomeProductName.setText(ls_product.get(position).product_name);
        holder.tvHomeDate.setText(ls_product.get(position).disclosure_date);
        if (ls_product.get(position).asset_class == Code.ProductCode.INCOME_TYPE) {
            holder.tvHomeNumber.setText(ls_product.get(position).wfsy.equals("")?"--":ls_product.get(position).wfsy);
            holder.tvHomeNumberTitle.setText(ctx.getString(R.string.million_income));
            holder.tvHomeRote.setText(ls_product.get(position).nhsyl.equals("")?"--":ls_product.get(position).nhsyl);
            holder.tvHomeRoteTitle.setText(ctx.getString(R.string.seven_date_income));
        } else if (ls_product.get(position).asset_class == Code.ProductCode.NET_TYPE) {
            holder.tvHomeNumber.setText(ls_product.get(position).navpu.equals("")?"--":ls_product.get(position).navpu);
            holder.tvHomeNumberTitle.setText(ctx.getString(R.string.unit_net));
            holder.tvHomeRote.setText(ls_product.get(position).zdf.equals("")?"--":ls_product.get(position).zdf);
            holder.tvHomeRoteTitle.setText(ctx.getString(R.string.day_up_or_down));
        } else if (ls_product.get(position).asset_class == Code.ProductCode.COMBINA_TYPE) {

        }
        holder.tvHomeLimit.setText(ls_product.get(position).attributes[0]);
        holder.tvHomeRisk.setText(ls_product.get(position).attributes[1]);

        int adapterPosition = holder.getAdapterPosition();
//        Log.d("-----", adapterPosition + "::" + mLastPosition + "::" + position);
        //设置加载item的动画
        if (adapterPosition > mLastPosition) {
            for (Animator anim : getAnimators(holder.itemView)) {
                anim.setDuration(mDuration).start();
                anim.setInterpolator(mInterpolator);
            }

        } else {
            ViewHelper.clear(holder.itemView);
        }
        mLastPosition = adapterPosition;
    }
        holder.itemView.setTag(position);
    }
    public void setInterpolator(Interpolator interpolator) {
        mInterpolator = interpolator;
    }
    public void setDuration(int duration) {
        mDuration = duration;
    }
    public void setFirstOnly(boolean firstOnly) {
        isFirstOnly = firstOnly;
    }
    public void setStartPosition(int start) {
        mLastPosition = start;
    }
     protected Animator[] getAnimators(View view) {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", mFrom, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", mFrom, 1f);
        return new ObjectAnimator[] { scaleX, scaleY };
    }

    @Override
    public int getItemCount() {

        int count = ls_product != null ? ls_product.size() : 0;
        if (VIEW_FOOTER != null) {
            count++;
        }

        if (VIEW_HEADER != null) {
            count++;
        }
        return count;
    }
    class ViewHolder extends RecyclerView.ViewHolder{

        public ImageView ivHomeHotIcon;
        public TextView tvHomeProductName;
        public TextView tvHomeDate;
        public TextView tvHomeNumber;
        public TextView tvHomeRote;
        public TextView tvHomeNumberTitle;
        public TextView tvHomeRoteTitle;
        public TextView tvHomeLimit;
        public TextView tvHomeRisk;
        public LinearLayout llProductItem;

        public ViewHolder(View itemView) {
            super(itemView);
            ivHomeHotIcon = (ImageView) itemView.findViewById(R.id.iv_home_hot_icon);
            tvHomeProductName = (TextView) itemView.findViewById(R.id.tv_home_product_name);
            tvHomeDate = (TextView) itemView.findViewById(R.id.tv_home_date);
            tvHomeNumber = (TextView) itemView.findViewById(R.id.tv_home_number);
            tvHomeRote = (TextView) itemView.findViewById(R.id.tv_home_rote);
            tvHomeNumberTitle = (TextView) itemView.findViewById(R.id.tv_home_number_title);
            tvHomeRoteTitle = (TextView) itemView.findViewById(R.id.tv_home_rote_title);
            tvHomeLimit = (TextView) itemView.findViewById(R.id.tv_home_limit);
            tvHomeRisk = (TextView) itemView.findViewById(R.id.tv_home_risk);
            llProductItem = (LinearLayout) itemView.findViewById(R.id.ll_product_item);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeaderView(position)) {
            return TYPE_HEADER;
        } else if (isFooterView(position)) {
            return TYPE_FOOTER;
        } else {
            return TYPE_NORMAL;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        try {
            if (mRecyclerView == null && mRecyclerView != recyclerView) {
                mRecyclerView = recyclerView;
            }
            ifGridLayoutManager();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private View getLayout(int layoutId) {
        return LayoutInflater.from(ctx).inflate(layoutId, null);
    }

    public void addHeaderView(View headerView) {
        if (haveHeaderView()) {
            throw new IllegalStateException("hearview has already exists!");
        } else {
            //避免出现宽度自适应
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            headerView.setLayoutParams(params);
            VIEW_HEADER = headerView;
            ifGridLayoutManager();
            notifyItemInserted(0);
        }

    }

    public void addFooterView(View footerView) {
        if (haveFooterView()) {
            throw new IllegalStateException("footerView has already exists!");
        } else {
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            footerView.setLayoutParams(params);
            VIEW_FOOTER = footerView;
            ifGridLayoutManager();
            notifyItemInserted(getItemCount() - 1);
        }
    }

    private void ifGridLayoutManager() {
        if (mRecyclerView == null) return;
        final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager.SpanSizeLookup originalSpanSizeLookup =
                    ((GridLayoutManager) layoutManager).getSpanSizeLookup();
            ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return (isHeaderView(position) || isFooterView(position)) ?
                            ((GridLayoutManager) layoutManager).getSpanCount() :
                            1;
                }
            });
        }
    }

    private boolean haveHeaderView() {
        return VIEW_HEADER != null;
    }

    public boolean haveFooterView() {
        return VIEW_FOOTER != null;
    }

    private boolean isHeaderView(int position) {
        return haveHeaderView() && position == 0;
    }

    private boolean isFooterView(int position) {
        return haveFooterView() && position == getItemCount() - 1;
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
